<?php

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */
extract($_POST);
if($actionLike == 'like') {
    $time = time();
    if($mysql->query("INSERT INTO ".$tb_prefix."post(post_media_id, post_poster_id, post_type,post_time) VALUES ('$media_id', '$user_id', '$type', '$time')")) {
        $likeID = $mysql->insert_id();
        $typeNoti = '';
        $u_action_id = $_SESSION['user_id'];
        $u_action_name = m_get_data('USER',$_SESSION['user_id']);
        switch ($type) {
            case 1 : $typeNoti = 2;break;
            case 2 : $typeNoti = 3;break;
        }
        if($type == 1) {
            $mediaID = $media_id;
            $rs = $mysql->fetch_array($mysql->query("SELECT m_poster, m_title FROM ".$tb_prefix."data WHERE m_id=$mediaID"));
            $ownerMedia = $rs['m_poster'];
            $media_name = $rs['m_title'];
        } else {
            $r = $mysql->fetch_array($mysql->query("SELECT ".$tb_prefix."comment.comment_media_id, ".$tb_prefix."data.m_poster, ".$tb_prefix."data.m_title 
                                                        FROM ".$tb_prefix."comment 
                                                        INNER JOIN ".$tb_prefix."data ON ".$tb_prefix."comment.comment_media_id = ".$tb_prefix."data.m_id
                                                        WHERE ".$tb_prefix."comment.comment_id=$media_id"));
            $mediaID = $r['comment_media_id'];
            $ownerMedia = $r['m_poster'];
            $media_name = $r['m_title'];
        }
        if($ownerMedia != $u_action_id)
            $mysql->query("INSERT INTO ".$tb_prefix."noti(obj_id,u_id, u_action_id, u_action_name, m_id, m_name, noti_type, noti_stt, noti_time) VALUES ('$likeID','$ownerMedia', '$u_action_id', '$u_action_name', '$mediaID', '$media_name','$typeNoti',1,'$time')");
        $rs = $mysql->query("(SELECT comment_poster as 'u_id', comment_time as 'time' 
                                FROM ".$tb_prefix."comment 
                                WHERE comment_media_id = $mediaID AND comment_poster <> $ownerMedia AND comment_poster <> $u_action_id)
                              UNION DISTINCT
                              (SELECT post_poster_id as 'u_id', post_time as 'time'
                                FROM ".$tb_prefix."post
                                WHERE post_media_id = $mediaID AND post_poster_id <> $ownerMedia AND post_poster_id <> $u_action_id)
                              ORDER BY time DESC LIMIT 0,15");
        while($r = $mysql->fetch_array($rs)) {
            $mysql->query("INSERT INTO ".$tb_prefix."noti(obj_id, u_id, u_action_id, u_action_name, m_id, m_name, noti_type, noti_stt, noti_time) VALUES ('$likeID','{$r['u_id']}', '$u_action_id', '$u_action_name', '$mediaID', '$media_name','$typeNoti',1,'$time')");
        }
        
        
        $rt['status'] = 'success';
        $s = $mysql->fetch_array($mysql->query("SELECT COUNT(*) as sumLike FROM ". $tb_prefix . "post WHERE post_media_id =". $media_id));
        $rt['sumLike'] = $s['sumLike'];
        if($type == 1) {
            $rt['button'] = '<div id="unlike">
                                <a href="#" onclick="return unlike('.$media_id.','.$u_action_id.', 1);" title="Thích hay không thích nữa"><span>Không thích</span></a>
                            </div>';
            $rt['sumLike'] -= 1;
            $listUser = '';
            $contentListLike = "bạn";
            $q = $mysql->query("SELECT p.post_poster_id, u.user_name  FROM ". $tb_prefix . "post AS p LEFT JOIN ". $tb_prefix . "user AS u ON p.post_poster_id = u.user_id WHERE p.post_media_id =" . $media_id . " AND p.post_type=1 AND p.post_poster_id <> $u_action_id ORDER BY post_time DESC LIMIT 0,2");
            if($mysql->num_rows($q)) {
                while($row = $mysql->fetch_array($q)) {
                    $rt['sumLike'] -= 1;
                    $contentListLike .= ", " . "<a href='" . url_link($row['post_poster_id'],$row['user_name'],'User') . "'>{$row['user_name']}</a>";
                }
            } else {
                $contentListLike = "bạn";
            }

            if($rt['sumLike'] > 0 && $contentListLike != '') {
                $listUser = "Có " . $contentListLike . " và ";
            } else {
                $listUser = "Có " . $contentListLike . " ";
            }
            $rt['listLike'] = $listUser;
            $rt['sumLike'] = ($rt['sumLike'] > 0) ? $rt['sumLike'] . " người " : '';
        } elseif($type == 2) {
            $rt['button'] = "<a href='#' onclick='return unlike_comment($media_id, {$_SESSION['user_id']}, 2)'>Không thích</a>";
        }
    } else {
        $rt['status'] = 'fail';
    }

} elseif($actionLike == 'unlike') {
    $rs = $mysql->fetch_array($mysql->query("SELECT * FROM ".$tb_prefix."post WHERE post_poster_id='$user_id' AND post_media_id='$media_id'"));
    $likeID = $rs['post_id'];
    $typeNoti = '';
    switch ($type) {
        case 1 : $typeNoti = 2;break;
        case 2 : $typeNoti = 3;break;
    }
	if($mysql->query("DELETE FROM ".$tb_prefix."post WHERE post_poster_id = '$user_id' AND post_media_id = '$media_id'; ")) {
        if($mysql->query("DELETE FROM ".$tb_prefix."noti WHERE obj_id='$likeID' AND noti_type='$typeNoti'")) {
            $s = $mysql->fetch_array($mysql->query("SELECT COUNT(*) as sumLike FROM ". $tb_prefix . "post WHERE post_media_id =". $media_id));
            $sumLike = $s['sumLike'];
            if($type == 1) {
                $listUser = '';
                $contentListLike = '';
                $q = $mysql->query("SELECT p.post_poster_id, u.user_name  FROM ". $tb_prefix . "post AS p LEFT JOIN ". $tb_prefix . "user AS u ON p.post_poster_id = u.user_id WHERE p.post_media_id =" . $media_id . " AND p.post_type=1 ORDER BY post_time DESC LIMIT 0, 3");
                if($mysql->num_rows($q)) {
                    while($row = $mysql->fetch_array($q)) {
                        $sumLike -= 1;
                        if($contentListLike != '')
                            $contentListLike .= ",";
                        $contentListLike .= " <a href='" . url_link($row['post_poster_id'],$row['user_name'],'User') . "'>{$row['user_name']}</a>";
                    }
                } else {
                    $contentListLike = "";
                }
                if($sumLike > 0 && $contentListLike != '') {
                    $listUser = "Có " . $contentListLike . " và ";
                } elseif($contentListLike != '') {
                    $listUser = "Có " . $contentListLike . " ";
                }
                
                if($listUser == '')
                    $listUser = "Chưa có ai";
                $rt['listLike'] = $listUser;
                $rt['sumLike'] = ($sumLike > 0) ? $sumLike . " người " : '';
                $rt['button'] = '<div id="like">
                                    <a href="#" onclick="return like('.$media_id.','.$_SESSION['user_id'].', 1);" title="Thích hay không thích nữa"><span>Thích</span></a>
                                </div>';
            } elseif($type == 2) {
                $rt['sumLike'] = $sumLike;
                $rt['button'] = "<a href='#' onclick='return like_comment($media_id, {$_SESSION['user_id']}, 2)'>Thích</a>";
            }
            $rt['status'] = 'success';
        } else {
            $rt['status'] = 'fail';
        }
	} else {
		$rt['status'] = 'fail';
	}	
} elseif($actionLike == 'showUserLike') {
    $html = "<div id='listUserLike'>
                <p>Những người thích chủ đề này : </p>
                <ul>";
    $q = $mysql->query("SELECT p.post_poster_id, u.user_name  FROM ". $tb_prefix . "post AS p LEFT JOIN ". $tb_prefix . "user AS u ON p.post_poster_id = u.user_id WHERE p.post_media_id =" . $idLike . " AND p.post_type=$typeLike ORDER BY u.user_name");
    while($row = $mysql->fetch_array($q)) {
        $html .= "<li><a href='".url_link($row['post_poster_id'],$row['user_name'],'User') . "'>{$row['user_name']}</a></li>";
    }
    $html .= "</ul></div>";
    $rt['status'] = 'success';
    $rt['html'] = $html;
} elseif ($actionPlaylist == 'change') {
    $playlist = m_get_data('PLAYLIST',$playlist_id);
    $playlist = trim($playlist,',');
    if ($playlist) {
        $$playlist_HTML = '';
        $q = $mysql->query("SELECT m_id, m_title, m_viewed, m_poster FROM ".$tb_prefix."data WHERE m_id IN (".$playlist.")");
        $i = 1;
        while ($r = $mysql->fetch_array($q)) {
                $song_ID 		= $r['m_id'];
                $song_TITLE 	= $r['m_title'];
                $user_ID 	= $r['m_poster'];
                $user_NAME1 =  m_get_data('USER',$user_ID);
                if($user_NAME1 == '')
                {
                        $user_NAME1 = 'DJ Other';
                }

                $user_URL	= url_link($user_ID,$user_NAME1,'User');	
                $m_viewed 	= $r['m_viewed'];	
                $song_TITLE1 = shortname($song_TITLE,40);				
                $song_URL		= url_link($song_ID,$song_TITLE,'nhac-san');
                
                $playlist_HTML .= "<li><p class='right'><span class='del'><a href=# onClick='removeFromPlaylist($song_ID);return false;' title='remove from Playlist'></a></span> </p> <p class='title'>$i. <a href='$song_URL' title='$song_TITLE'>$song_TITLE1</a></p><p class='user'>Đăng bởi: <a href='$user_URL'>$user_NAME1</a></p></li>";
                $i++;
        }
        $playlist_URL	= url_link($playlist_id,$user_name,'Playlist');		
        $rt['playAll'] = ($_SESSION['user_id'])?"<p style='margin-top:20px;margin-left:85px;' id='playAllPlaylist'> <a style='color:#89014b;' href=$playlist_URL><b>Nghe Playlist</b></a></p>":'';
        $rt['html'] = $playlist_HTML;
    } else {
        $rt['html'] = "<li><p class='title'>Playlist chưa có bài hát</a></p></li>";
        $rt['playAll'] = '';
    }
} 
echo json_encode($rt);

?>
